import {
  login,
  logout,
  getInfo,
  gotoLogin,
  getRoles
} from '@/api/sys/login/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    permissions: [],
    loginUserInfo: {},
    simpleLock: {}
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: state => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_PERMISSIONS: (state, permissions) => {
    state.permissions = permissions
  },
  SET_LOGINUSERINFO: (state, userInfo) => {
    state.loginUserInfo = userInfo
  },

  SET_SIMPLELOCK: (state, simpleLock) => {
    state.simpleLock = simpleLock
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { account, pwd } = userInfo
    return new Promise((resolve, reject) => {
      login({ account: account.trim(), pwd: pwd })
        .then(response => {
          const { data } = response
          commit('SET_TOKEN', data)
          setToken(data)
          resolve()
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  //保存第三方门户网站的token信息
  saveToken({ commit }, token) {
    commit('SET_TOKEN', token)
    setToken(token)
  },

  // 获取用户信息
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo()
        .then(res => {
          const user = res.data.userInfo
          //默认头像给空
          user['avatar'] = ''
          //头像
          const avatar = process.env.VUE_APP_BASE_API + user.avatar
          //     ? require('@/assets/images/profile.jpg')
          //     : process.env.VUE_APP_BASE_API + user.avatar
          // 验证返回的roles是否是一个非空数组
          if (res.data.roleList && res.data.roleList.length > 0) {
            commit('SET_ROLES', res.data.roleList)
            commit('SET_LOGINUSERINFO', user)
            commit('SET_PERMISSIONS', res.data.permission)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit('SET_SIMPLELOCK', res.data.simpleLock)
          commit('SET_NAME', user.userName)
          commit('SET_AVATAR', avatar)
          resolve(res)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token)
        .then(() => {
          removeToken() // must remove  token  first
          resetRouter()
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          commit('RESET_STATE')
          commit('SET_LOGINUSERINFO', {})
          resolve()
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  },

  //跳转到门户登录网址
  gotoLogin({ commit }) {
    removeToken() // must remove  token  first
    commit('RESET_STATE')
    gotoLogin()
  },

  assignmentPermissions({ commit }, permissions) {
    commit('SET_PERMISSIONS', permissions)
  },

  // 重新设置用户信息
  resetUserInfo({ commit },userInfo) {
    commit('SET_LOGINUSERINFO', userInfo)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
